import axios from "axios";
import {ElMessage} from "element-plus";

const base_url = 'https://helper.jx3box.com/'

const __axios = axios.create({
    baseURL: base_url,
    timeout: 4000
})
__axios.interceptors.response.use(response => {
    if (response.status >= 500) {
        ElMessage.error(response.statusText)
        Promise.reject(response.statusText)
    }

    if (response.data.code === 500) {
        ElMessage.error(response.data.message)
        Promise.reject(response.data.code)
    }
    return response
})
export const http4 = {
    get(url: string, data: any) {
        return new Promise((resolve, reject) => {
            __axios.request({
                url: url,
                data: data,
                params: data,
                headers: {
                    // token: localStorage.getItem('token') === null ? '' : localStorage.getItem('token')
                }
            }).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    },
    post(url: string, data: any) {
        return new Promise((resolve, reject) => {
            __axios.request({
                url: url,
                headers: {
                    // token: localStorage.getItem('token') === null ? '' : localStorage.getItem('token')
                },
                data: data,
                method: 'post',
                params: data
            }).then(res => {
                // console.log(res);
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })

            // __axios.post(url, data, {
            //     headers: {
            //         token: localStorage.getItem('token') === null ? '' : localStorage.getItem('token')
            //     }
            // }).then(res => {
            //     resolve(res.data)
            // }).catch(err => {
            //     reject(err)
            // })
        })
    }
}
